<?php

/**
 * Replace dirname(__FILE__) by __DIR__
 */
define('ROOT_DIR', dirname(__FILE__));
define('CODE_DIR', ROOT_DIR . '/code');

$paths = array(
    get_include_path(),
    realpath(ROOT_DIR . '/../lib')
);

set_include_path(implode(PATH_SEPARATOR, $paths));

require 'Fete/Loader.php';
Fete_Loader::registerAutoload();

require '../configs/db.php';
require '../configs/php.php';

/**
 * @return Fete_Db
 */
function &db()
{
    if (!isset($GLOBALS['_db'])) {
        $db = new Fete_Db(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        $db->setConnectionEncoding('utf8')->enableAutoReconnection(true);
        $GLOBALS['_db'] = $db;
        return $db;
    }
    return $GLOBALS['_db'];
}

/**
 * @return Fete_Logger
 */
function &logger()
{
    if (!isset($GLOBALS['_logger'])) {
        $logger = new Fete_Logger(ROOT_DIR . '/logs');
        $GLOBALS['_logger'] = $logger;
        return $logger;
    }

    return $GLOBALS['_logger'];
}

/**
 * @return Fete_Cache
 */
function &cacher()
{
    if (!isset($GLOBALS['_cache'])) {
        $GLOBALS['_cache'] = Fete_Cache::getInstance(ROOT_DIR . '/caches');
    }

    return $GLOBALS['_cache'];
}

function baseUrl()
{
    return Fete_Kernel::getInstance()->getBaseUrl();
}


Fete_Language::getInstance()->loadFile(ROOT_DIR . '/../languages/' . Lang::getCode() . '.php');

Fete_Front::getInstance()->setActionDir(ROOT_DIR . '/code')
    ->setViewDir(ROOT_DIR . '/tpl')
    ->run();

?>
